<?php
class ModelCatalogPage extends Model {
	public function addPage($data) {

		$page_id = $this->db->insert('page', $data);

		foreach ($data['page_content'] as $language_id => $value) {
			$value['page_id'] = $page_id;
			$value['language_id'] = $language_id;
			$this->db->insert('page_content', $value);
		}

		if (isset($data['page_store'])) {
			foreach ($data['page_store'] as $store_id) {
				$this->db->query("INSERT INTO  @@page_to_store SET page_id = $page_id, store_id = " . (int)$store_id);
			}
		}

		if (isset($data['page_layout'])) {
			foreach ($data['page_layout'] as $store_id => $layout) {
				if ($layout) {
					$this->db->query("INSERT INTO  @@page_to_layout SET page_id = $page_id, store_id = " . (int)$store_id . ", layout_id = " . (int)$layout['layout_id']);
				}
			}
		}

		return $page_id;
	}

	public function editPage($page_id, $data) {
		$this->db->update('page', $data, "page_id=$page_id");
		$this->db->query("DELETE FROM  @@page_content WHERE page_id = $page_id");

		foreach ($data['page_content'] as $language_id => $value) {
			$value['page_id'] = $page_id;
			$value['language_id'] = $language_id;
			$this->db->insert('page_content', $value);
		}

		$this->db->query("DELETE FROM  @@page_to_store WHERE page_id = $page_id");

		if (isset($data['page_store'])) {
			foreach ($data['page_store'] as $store_id) {
				$this->db->query("INSERT INTO  @@page_to_store SET page_id = $page_id, store_id = " . (int)$store_id);
			}
		}

		$this->db->query("DELETE FROM  @@page_to_layout WHERE page_id = $page_id");

		if (isset($data['page_layout'])) {
			foreach ($data['page_layout'] as $store_id => $layout) {
				if ($layout['layout_id']) {
					$this->db->query("INSERT INTO  @@page_to_layout SET page_id = $page_id, store_id = " . (int)$store_id . ", layout_id = " . (int)$layout['layout_id']);
				}
			}
		}
	}

	public function deletePage($page_id) {
		$page_id = (int) $page_id;
		$this->db->query("DELETE FROM  @@page WHERE page_id = $page_id");
		$this->db->query("DELETE FROM  @@page_content WHERE page_id = $page_id");
		$this->db->query("DELETE FROM  @@page_to_store WHERE page_id = $page_id");
		$this->db->query("DELETE FROM  @@page_to_layout WHERE page_id = $page_id");
	}

	public function getPage($page_id) {
		return $this->db->queryOne("SELECT DISTINCT * FROM  @@page WHERE page_id = " . (int)$page_id);
	}

	public function getPages($filter = null) {
		$sql = 'SELECT p.*, pd.title FROM @@page p LEFT JOIN  @@page_content pd ON (p.page_id = pd.page_id) WHERE pd.language_id = ' . (int)C('config_language_id');
		
		if ($filter) {
			$sql .= $filter->sort('p.sort_order');
			$sql .= $filter->limit();
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getPageContents($page_id) {
		return $this->db->queryArray("SELECT language_id,title,content FROM  @@page_content WHERE page_id = " . (int)$page_id, 'language_id');
	}

	public function getPageStores($page_id) {
		return $this->db->queryArray('SELECT store_id FROM @@page_to_store WHERE page_id = ' . (int)$page_id);
	}

	public function getPageLayouts($page_id) {
		return $this->db->queryArray("SELECT * FROM  @@page_to_layout WHERE page_id = " . (int)$page_id, 'store_id', 'layout_id');
	}

	public function getTotalPages() {
      	return $this->db->queryOne("SELECT COUNT(*) AS total FROM  @@page");
	}

	public function getTotalPagesByLayoutId($layout_id) {
		return $this->db->queryOne("SELECT COUNT(*) AS total FROM  @@page_to_layout WHERE layout_id = " . (int)$layout_id);
	}
}
?>